package com.sgb.kjwl.view.widget;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.view.adapter.CategoryLeftAdapter3;
import com.swgk.core.view.adapter.CategoryRightAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.SectionAdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class CategoryPopWindow3 extends BasePopWindow<String> {
    private RecyclerView leftRecyclerView;
    private XRecyclerView rightRecyclerView;
    private CategoryLeftAdapter3 leftAdapter;
    private CategoryRightAdapter3 rightAdapter;
    private TextView tvCancel;
    private TextView tvOk;
    private String cid3 = "";
    private String cid1 = "";
    private DictWindow3.OnCloseBack onCloseBack;

    public CategoryPopWindow3(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_category3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        leftRecyclerView = holder.getPop().findViewById(R.id.left_recycler);
        rightRecyclerView = holder.getPop().findViewById(R.id.right_recycler);
        tvCancel = holder.getPop().findViewById(R.id.tv_cancel);
        tvOk = holder.getPop().findViewById(R.id.tv_ok);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultCallBack != null) {
                    onResultCallBack.result(cid3);
                }
                if(onResultClick!=null){
                    onResultClick.result(cid1,cid3,null);
                }
                dissMiss();
            }
        });

        if (leftAdapter == null) {
            leftAdapter = new CategoryLeftAdapter3(mContext, null);
        }
        leftRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        leftRecyclerView.setAdapter(leftAdapter);
//        leftRecyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
//                0,
//                0,
//                mContext.getResources().getDimension(R.dimen.dp1), mContext.getResources().getColor(R.color.color_EDEDED)));
        leftAdapter.setItemSelect(true);
        leftAdapter.setOnItemClick(new AdapterOnItemClick<MenuEntity<MenuEntity<MenuEntity>>>() {
            @Override
            public void onItemClick(MenuEntity<MenuEntity<MenuEntity>> menuEntity3MenuEntity3, int position) {
                cid1 = menuEntity3MenuEntity3.getId();
                cid3 = "";
                showRightAdapter(menuEntity3MenuEntity3.getDatas());
                updatedata();
            }
        });
        if (rightAdapter == null) {
            rightAdapter = new CategoryRightAdapter3(mContext, null);
        }
        rightAdapter.setItemSelect(true);
        rightRecyclerView.setSectionedRecyclerViewAdapter(rightAdapter);
        rightRecyclerView.setLoadingMoreEnabled(false);
        rightRecyclerView.setPullRefreshEnabled(false);
        GridLayoutManager rightManager = new GridLayoutManager(mContext, 3);
        rightRecyclerView.setLayoutManager(rightManager);
        rightRecyclerView.setAdapter(rightAdapter);
        rightAdapter.setOnItemClickListener(new SectionAdapterOnItemClick<MenuEntity>() {
            @Override
            public void onItemClick(MenuEntity menuEntity3, int section, int position) {
                cid3 = menuEntity3.getId();
                updatedata();
            }
        });


        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightAdapter.setState(false);
                if(((TextView)view).getText().toString().equals("重置")){
                    leftAdapter.setDefItem(0);
                    cid1 = "";
                    cid3 = "";
                }
                if(onResultClick!=null){//会员价筛选重置
                    onResultClick.result(null, null, null);
                    dissMiss();
                }
                updatedata();

            }
        });
    }

    public void setDatas(List<MenuEntity<MenuEntity<MenuEntity>>> datas) {
        leftAdapter.setDatas(datas);
        leftAdapter.setDefItem(0);
        if (datas.size() < 0) {
            return;
        }



        showRightAdapter(datas.get(0).getDatas());
    }

    private void updatedata(){
        if(!TextUtils.isEmpty(cid1)||!TextUtils.isEmpty(cid3)){
            if(tvCancel!=null){
                tvCancel.setText("重置");
            }
        }else{
            if(tvCancel!=null){
                tvCancel.setText("取消");
            }
        }
    }

    public void showRightAdapter(List<MenuEntity<MenuEntity>> data) {
        rightAdapter.setState(false);
        rightAdapter.setData(data);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCloseBack != null) {
            onCloseBack.dismiss();
        }
    }

    public interface OnCloseBack {
        void dismiss();
    }

    public void setOnCloseBack(DictWindow3.OnCloseBack onCloseBack) {
        this.onCloseBack = onCloseBack;
    }
}
